package com.ruoyi.web.controller.initData;

import cn.hutool.core.util.IdUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.system.domain.dao.SysDeptDao;
import com.ruoyi.system.mapper.OaMapper;
import com.ruoyi.system.mapper.SysDeptDaoMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Component
public class DeptInit {

    @Autowired
    private OaMapper oaMapper;
    @Resource
    private SysDeptDaoMapper sysDeptDaoMapper;

    /**
     *  初始化 部门
     */
    public void initDept() {
        List<Map<String, String>> list = oaMapper.listData("ent_t_dep");
        Map<String, String> map = new HashMap<>();  // <depCode,id>，用于替换depId
        if (list != null && list.size() > 0) {
            SysDeptDao rootDept = new SysDeptDao();
            rootDept.setDeptId(IdUtil.getSnowflake(1,1).nextIdStr());
            rootDept.setParentId("0");
            rootDept.setAncestors("0");
            rootDept.setDeptName("甘肃省第八建设集团有限责任公司");
            rootDept.setSimpleName("甘肃省第八建设集团有限责任公司");
            rootDept.setStatus("0");
            rootDept.setOrgType("0");    // 机构类型，0-单位/公司，1-部门/中心，2-科室/处室，3-班组/小组
            rootDept.setDelFlag("0");
            rootDept.setOrderNum(800000);
            rootDept.setOrgCode("800000");
            sysDeptDaoMapper.insert(rootDept);
            map.put("800000", rootDept.getDeptId());

            list.stream().forEach(item -> {
                if ("0".equals(String.valueOf(item.get("is_valid")))) {
                    return;
                }
                SysDeptDao sysDept = new SysDeptDao();
                sysDept.setOldData(String.valueOf(item.get("ent_t_dep_uuid")));
                String depCode = item.get("dep_code");
                sysDept.setOrgCode(depCode);
                if (!map.containsKey(depCode)) {
                    map.put(depCode, IdUtil.getSnowflake(1,1).nextIdStr());
                }
                depCode = map.get(depCode);
                sysDept.setDeptId(depCode);
                String parentDepCode = item.get("parent_dep_code");
                if ("800001".equals(parentDepCode)) {
                    parentDepCode="800000";
                }
                if (!map.containsKey(parentDepCode)) {
                    map.put(parentDepCode, IdUtil.getSnowflake(1,1).nextIdStr());
                }
                parentDepCode = map.get(parentDepCode);
                sysDept.setParentId(parentDepCode);
//                if (sysDept.getDeptId().equals(sysDept.getParentId())) {
//                    sysDept.setParentId("0");
//                }
                sysDept.setDeptName(item.get("dep_name_chn"));
                sysDept.setSimpleName(item.get("formula_alias"));
                sysDept.setOrderNum(Integer.parseInt(String.valueOf(item.get("sort_num"))));
                sysDept.setStatus("0");
                sysDept.setOrgType("1");    // 机构类型，0-单位/公司，1-部门/中心，2-科室/处室，3-班组/小组
                if ("10160".equals(item.get("dep_type"))) {
                    sysDept.setOrgType("0");
                } else if ("10161".equals(item.get("dep_type"))) {
                    sysDept.setOrgType("1");
                } else if ("10162".equals(item.get("dep_type")) || "10163".equals(item.get("dep_type"))) {
                    sysDept.setOrgType("0");
                }
                sysDept.setDelFlag("0");
                sysDept.setOldData(String.format("depUuid=%s,dutyUuid=%s,topLeaderDutyUuid=%s",
                        item.get("ent_t_dep_uuid"),
                        item.get("ent_t_duty_uuid"),item.get("top_leader_duty_uuid")));
                sysDeptDaoMapper.insert(sysDept);
            });
        }
        List<SysDeptDao> sysDeptList = sysDeptDaoMapper.selectList(new QueryWrapper<>());
        if (sysDeptList != null && sysDeptList.size() > 0) {
            sysDeptList.stream().filter(item-> DateUtils.getDate().equals(DateUtils.parseDateToStr("yyyy-MM-dd", item.getCreateTime())))
                    .forEach(item->{
                        StringBuilder sb = new StringBuilder("0");
                        String parentId = item.getParentId();
                        while (true){
                            SysDeptDao temp = findSysDeptByDepId(parentId, sysDeptList);
                            if (temp!=null) {
                                sb.append(",").append(temp.getDeptId());
                                parentId = temp.getParentId();
                            }else{
                                break;
                            }
                        }
                        item.setAncestors(sb.toString());
                        sysDeptDaoMapper.updateById(item);
                    });
        }
    }

    private SysDeptDao findSysDeptByDepId(String depId,List<SysDeptDao> sysDeptList) {
        if (sysDeptList != null && sysDeptList.size() > 0) {
            for (SysDeptDao sysDept : sysDeptList) {
                if (sysDept.getDeptId().equals(depId)) {
                    return sysDept;
                }
            }
        }
        return null;
    }
}
